<?php
##############################################
#
#   会员申请
# @ Time : 2017-1-3 - 下午10:45:24
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Admin\Controller;
use Admin\Model\AdminBaseModel; 
use Admin\Model\ApplyUserModel;


class ApplyUserController extends AdminBaseController {
	
	protected $_apply;
	
	public function _initialize(){
		parent::_initialize();
		$this->_apply = new ApplyUserModel();
	}
	
	/**
	 * 活动列表
	 */
	public function applyList(){
	    $data = $this->_apply->getApply(I('full_name'), I('phone_number'), I('email'), I('apply_type'), I('status'));
	    $this -> assign('apply', $data[0]);
	    $this -> assign('page', $data[1]);
	    $this -> display('index');
	}
	
	/**
	 * 查看申请
	 */
 	public function addApply(){
		 if(IS_GET){
		    $id = I('id');
		    $act = I('get.act');
		    if($id && $act == 'edit'){
		        $data = $this->_apply->getApplyFind($id);
		        $this->assign('id', $id);
		        $this->assign('apply', $data);
		    }
		    $this->assign('act', $act);
		    $this->display('add');
		}else if(IS_POST){
		    $data = array(
		        'id' => I('id'),
		        'user_id' => I('user_id'),
		        'apply_type' => I('apply_type'),
		        'status' => I('status'),
		        'refuse' => I('refuse')
		    );
		    if($data['status'] == 3){
		        $data['refuse'] = htmlspecialchars($data['refuse']);
		    }else if($data['status'] == 2){
		        //审核通过修改用户类型
		        M('users')->where("id = {$data['user_id']}")->setField('user_type', $data['apply_type']);
		        $data['refuse'] = '';
		    }else if($data['status'] == 1){
		        $data['refuse'] = '';
		    }
		   	$admin_base_model = new AdminBaseModel('apply_user');
		   	$act = I('post.act');
		   	if($act == 'add'){
		   	    $result = $admin_base_model->addData($data);
		   	    if($result)
		   	        $this->success('添加成功', U('applyList'), 1);
		   	    else
		   	        $this->error('操作失败', '', 1);
		   	}elseif($act == 'edit'){
		   	    $id = I('id');
		   	    if($this->_apply->editApply($id, $data) !== false)
		   	        $this->success('修改成功', U('applyList'), 1);
		   	    else
		   	        $this->error('操作失败', '', 1);
		   	}
    	} 
	}

	/**
	 * 全选删除用户
	 */
    public function deleteAllUsers(){
	    if(!IS_POST) return;
	    $result = $this->_users->deleteAllUsers($_POST);
	    if($result)
			$this->success('删除成功', U('usersList'), 1);
		else
			$this->error('操作失败', '', 1);
	}
	
	/**
	 * 删除申请
	 */
	public function deleteApply(){
		if(!IS_AJAX) return;
		$result = $this->_apply->deleteApply(intval($_GET['id']));
		if($result)
			$this->ajaxReturn(array('status' => 1,'message' => '删除成功'));
		else
			$this->ajaxReturn(array('status' => 2,'message' => '操作失败'));
	}
	
	
	/**
	 * 操作
	 */
	public function operate(){
	    $operate = I('post.operate');
	    $selected_id_arr = I('post.apply_id');
	    if(!in_array($operate,array('del','ok_status','not_status')) || !is_array($selected_id_arr)){
	        $this->error('非法操作');
	    }
	    $map['id'] = array('in', $selected_id_arr);
	    $apply = M('apply_user');
	    switch ($operate){
	        case 'del':
	            $row = $apply->where($map)->delete();
	            break;
	        case 'ok_status':
	            $data = array('status'=>2);
	            $row = $apply->where($map)->save($data);
	            //通过修改用户类型
	            foreach ($selected_id_arr as $k => $v){
	                $apply_data = $apply->field('apply_type, user_id')->where("id = $v")->find();
	                M('users')->where("id = {$apply_data['user_id']}")->setField('user_type', $apply_data['apply_type']);
	            }
	            break;
	        case 'not_status':
	            $data = array('status'=>3);
	            $row = $apply->where($map)->save($data);
	            break;
	    }
	    if(!$row)
	        $this->error('操作失败');
	    else
	        $this->success('操作成功', U('applyList'), 1);
	
	}
	
	//当前用户的活动列表
	public function usersActivityList(){
	    $user_id = I('id');
	    
	}

}



	
	